//========================================================================
// *作者：海盗猫  主页：ilplay.com
// *脚本：ToolBuildBase
// *描述：
// *?2021 MYJL . All rights reserved.
//* ======================================================================
#if(UNITY_EDITOR)
using System;
using System.IO;
using UnityEngine;
using YooAsset.Editor;
using YooAsset;
using UnityEditor;
namespace HDMCode.Hotfix
{
    public class ToolBuildBase
    {
        /// <summary>
        /// 设置包名
        /// </summary>
        private string _PackageName;

        /// <summary>
        /// 输出路径
        /// </summary>
        private string _ServerSavePath;

        /// <summary>
        /// StreamingAssets资源路径
        /// </summary>
        private string _StreamingPath;

        /// <summary>
        /// 是否保存至StreamingAssets的布尔变量
        /// </summary>
        private bool _ISOutPutStreaming;

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="PackageName"></param>
        /// <param name="OutPutStreamingAssets"></param>
        public ToolBuildBase(string PackageName, bool OutPutStreamingAssets)
        {
            this._ISOutPutStreaming = OutPutStreamingAssets;
            _PackageName = PackageName;
        }

        /// <summary>
        /// 清理文件夹
        /// </summary>
        public void ClearFolder()
        {
            if (Directory.Exists(_ServerSavePath)) Directory.Delete(_ServerSavePath, true);
            if (Directory.Exists(_StreamingPath))
            {
                AssetDatabase.DeleteAsset(_StreamingPath);
            }
            Directory.CreateDirectory(_StreamingPath);
        }

        /// <summary>
        /// 复制文件到文件夹
        /// </summary>
        public void CopyFile()
        {
            DirectoryInfo directory = new DirectoryInfo(_ServerSavePath);
            foreach (FileInfo file in directory.GetFiles())
            {
                File.Copy(file.FullName, $"{DrawMeun.drawUISetting.AssetsOutPutPath}/{file.Name}", true);
                if (_ISOutPutStreaming)
                {
                    File.Copy(file.FullName, $"{_StreamingPath}/{file.Name}", true);
                }
            }
        }

        /// <summary>
        /// 创建加密类实例
        /// </summary>
        public IEncryptionServices CreateEncryptionInstance(string PackageName, EBuildPipeline EBuildPipeline)
        {
            var encyptionClassName = AssetBundleBuilderSetting.GetPackageEncyptionClassName(PackageName, EBuildPipeline);
            var encryptionClassTypes = EditorTools.GetAssignableTypes(typeof(IEncryptionServices));
            var classType = encryptionClassTypes.Find(x => x.FullName.Equals(encyptionClassName));
            if (classType != null)
                return (IEncryptionServices)Activator.CreateInstance(classType);
            else
                return null;
        }

        /// <summary>
        /// 获取版本号
        /// </summary>
        /// <returns></returns>
        public string GetDefaultPackageVersion()
        {
            int totalMinutes = DateTime.Now.Hour * 60 + DateTime.Now.Minute + DateTime.Now.Second;
            string version = DateTime.Now.ToString("yyyy-MM-dd") + "-" + totalMinutes;
            SetPath(version);
            return version;
        }

        /// <summary>
        /// 设置资源输出路径
        /// </summary>
        /// <param name="version"></param>
        public void SetPath(string version)
        {
#if UNITY_ANDROID
            _ServerSavePath = $"{YooAsset.Editor.AssetBundleBuilderHelper.GetDefaultBuildOutputRoot()}/Android/{_PackageName}/{version}";
#elif UNITY_IOS
            _ServerSavePath = $"{YooAsset.Editor.AssetBundleBuilderHelper.GetDefaultBuildOutputRoot()}/iOS/{_PackageName}/{version}";
#elif UNITY_WEBGL
            _ServerSavePath = $"{YooAsset.Editor.AssetBundleBuilderHelper.GetDefaultBuildOutputRoot()}/WebGL/{_PackageName}/{version}";
#endif
            _StreamingPath = $"{Application.streamingAssetsPath}/yoo/{_PackageName}";
        }
    }
}
#endif